Skip to content

fix: Keep Bgm stopped when stop is called while play is loading - #4256

Merged
ufrshubham merged 3 commits into
flame-engine:mainfrom
matthewalberto:fix/bgm-stop-during-play
Nov 28, 2026
Merged

ufrshubham merged 3 commits into
flame-engine:mainfrom
matthewalberto:fix/bgm-stop-during-play

Conversation

@matthewalberto

Copy link
Copy Markdown
Contributor

Description

Bgm.play() awaits release, setReleaseMode, setVolume and setSource before it calls resume(). A stop() during those awaits stopped the player, and then play() resumed it, so the music started anyway and isPlaying ended up true. Two overlapping play() calls both resumed the player the same way.

Now each play() takes an id from a counter that stop() and any later play() also move on. After setSource, a play() whose id is no longer the latest returns without calling resume(). isPlaying is now set before awaiting resume(), so a stop() that lands during resume() also leaves it false.

I went with the counter rather than a lock (synchronized is already a dependency), because with a lock stop() would have to wait for the track to load and start before stopping it, so the music could be heard for a moment.

Testing: a new test/bgm_test.dart with a mocked AudioPlayer whose setSource waits until the test completes it. Four tests: stop while play is loading, stop while the music is starting, play after stop, and play while another play is loading. Three of them fail on main and all pass with this PR (the whole flame_audio suite: 2 passed and 3 failed on main, 5 passed here). I also broke each part of the fix on its own (no id check, stop() not moving the counter, play() not taking a new id, isPlaying set after resume() again), and a test failed each time. dart format, flutter analyze for flame_audio and cspell are clean. Not tested: on a device or in a running game, and on Linux or macOS.

Checklist

  • I have followed the Contributor Guide when preparing my PR.
  • I have updated/added tests for ALL new/updated/fixed functionality.
  • I have updated/added relevant documentation in docs and added dartdoc comments with ///.
  • [-] I have updated/added relevant examples in examples or docs.

Breaking Change?

  • Yes, this PR is a breaking change.
  • No, this PR is not a breaking change.

Related Issues

Closes #4255

Bgm.play awaits release, setReleaseMode, setVolume and setSource on the
shared player before it calls resume. A stop() called during those awaits
stopped the player, and then play went on to resume it, so the music
started anyway and isPlaying ended up true. Two overlapping play calls
both resumed the player in the same way.

play now takes an id, and stop and any later play change it, so a play
that has been stopped or replaced returns before resume. isPlaying is
set before awaiting resume, so a stop during resume also leaves it false.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Overlapping play calls can let a stale load overwrite the active audio source, and a failed resume can leave isPlaying incorrectly true.

2 open issues
What changed in this PR

Updates Flame Audio’s Bgm playback flow to address #4255, where a pending play() could restart music after stop().

Changes:

  • Adds request tracking to skip resume() for stopped or superseded playback requests.
  • Sets isPlaying before awaiting resume() so a concurrent stop leaves it false.
  • Adds four regression tests covering stop during loading or startup, playback after stop, and overlapping playback requests.
File Description
packages/​flame_audio/​test/​bgm_test.dart Adds mocked-player tests for playback and stop races.
packages/​flame_audio/​lib/​bgm.dart Adds request tracking, adjusts playback-state timing, and documents cancellation behavior.

Review effort: Balanced

Comment on lines +80 to +82
if (playId != _playId) {
return;
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, but this one predates this PR: before it the older play also resumed, so the wrong track played outright, and this PR only stops that extra resume. Serialising setup with a lock changes how overlapping play calls behave, so I would rather open it as a follow-up. Happy to add it here instead if you prefer.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is better to fix this separately.

Comment thread packages/flame_audio/lib/bgm.dart Resolved
play sets isPlaying before awaiting resume, so that a stop during resume
leaves it false. If resume throws, play completed with the error but left
isPlaying true although no music started. Reset it on failure when this
play is still the current one, then rethrow.
@ufrshubham
ufrshubham enabled auto-merge (squash) November 28, 2026 14:00
@ufrshubham
ufrshubham merged commit 393636e into flame-engine:main Nov 28, 2026
9 checks passed
matthewalberto added a commit to matthewalberto/matthewalberto that referenced this pull request Nov 29, 2026
spydon pushed a commit to ywt-helloworld/flame that referenced this pull request Dec 1, 2026
…me-engine#4261)

Follow-up to flame-engine#4256, where this was left to be fixed separately.

`Bgm.play()` loads into one shared `AudioPlayer`. Two overlapping calls
loaded at the same time, so when the older track finished loading last,
its `setSource` reached the player after the newer track had started,
and replaced it.

Now a lock lets one `play()` at a time release the player and load its
source (`synchronized` is already a dependency of `flame_audio`). A
`play()` that was replaced or stopped while it waited for the lock loads
nothing. `stop()` does not take the lock, so it still takes effect at
once. `dispose()` now also moves the play counter on, so a waiting
`play()` does not touch a disposed player.

The cost: a new track starts to load only after the one before it has
finished loading. If that earlier load hangs, the new track waits until
`audioplayers` times it out (`AudioPlayer.preparationTimeout`). The
other way I saw was to keep loading both at once and load the newest
source again after a late one lands, which avoids the wait but restarts
the newest track.

Testing: five tests in `test/bgm_test.dart` are new or changed. The
overlap test now checks which source the player ends up holding, and the
others cover a replaced waiting play, `stop()` and `dispose()` while a
play waits, and a failed load not blocking the next one. Four of them
fail on `main`. The failed-load one passes there, as it guards the new
lock. I broke each part of the fix on its own and a test failed each
time. The whole `flame_audio` suite passes (10 tests), and `dart
format`, `flutter analyze` and cspell are clean. I also ran a throwaway
test with a real `AudioPlayer` and a fake platform: on `main` the last
source the platform receives is the old track, and with this change it
is the new one. Not tested: on a device or in a running game, and on
Linux or macOS.
spydon pushed a commit that referenced this pull request Dec 4, 2026
…oading (#4307)

Follow-up to #4256 and #4261, for `pause()` and the lifecycle handler.

When `pause()` was called, or the app left the foreground, while
`Bgm.play()` was still loading its track, `play()` still called
`resume()` once the track had loaded, so the music started after the
game paused it, or played in the background.

Now `pause()` sets a flag that `play()` checks after loading: the track
stays loaded and `isPlaying` stays false, and `resume()` starts it.
`play()` and `resume()` clear the flag. `didChangeAppLifecycleState`
also records whether the app is in the resumed state. A track that
finishes loading while it is not sets `isPlaying` but is not started,
and the `resumed` handler starts it.

For that, the `resumed` handler now restarts a player in
`PlayerState.stopped` as well as `paused`, when `isPlaying` is true. In
`audioplayers` 6.8.1 the state is set in Dart: `release()` calls
`stop()`, which sets `stopped`, and `setSource()` leaves the state
alone. So a loaded track that was never started is `stopped` if the app
left the foreground before `play()` released the player, and `paused` if
it left after.

Testing: 8 new tests in `test/bgm_test.dart`. 4 fail on `main` (pause
while loading, resume after that pause, and the two background cases).
The other 4 guard the new flags (play after a pause, pause and resume
both during the load, the app coming back before the load ends, and the
app coming back after a `pause()`). I broke each part of the fix on its
own (10 changes) and a test failed each time. The whole `flame_audio`
suite passes (18 tests), and `dart format`, `flutter analyze` and cspell
are clean. I also ran a throwaway test with a real `Bgm` and
`AudioPlayer` and a fake platform: on `main` the platform gets `resume`
after the pause in all three cases, and with this change it gets
`resume` only after `resume()` or the app coming back. Not tested: on a
device or in a running game.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[flame_audio] Bgm keeps playing when stop() is called while play() is still loading

3 participants